package com.sas.web;

import java.util.Arrays;
import java.util.List;
import java.util.Locale;

import org.apache.wicket.Request;
import org.apache.wicket.RequestCycle;
import org.apache.wicket.Response;
import org.apache.wicket.Session;
import org.apache.wicket.model.ResourceModel;
import org.apache.wicket.protocol.http.WebApplication;
import org.apache.wicket.protocol.http.WebRequest;
import org.apache.wicket.spring.injection.annot.SpringComponentInjector;
import org.hibernate.SessionFactory;
import org.springframework.beans.factory.annotation.Autowired;

import com.sas.web.common.HibernateWebRequestCycle;

/**
 * Application object for your web application. If you want to run this application without deploying, run the Start class.
 * 
 * @see com.sas.Start#main(String[])
 */
public class SasApplication extends WebApplication{
	
	/** Relevant locales wrapped in a list. */
    public static final List<Locale> LOCALES = Arrays.asList(new Locale[] { Locale.ENGLISH,
            Locale.SIMPLIFIED_CHINESE, new Locale("es", "AR")});
	
	private final SessionFactory sessionFactory;
    /**
     * Constructor
     */
	@Autowired
	public SasApplication(SessionFactory sessionFactory) {
		this.sessionFactory = sessionFactory;
	}
	
	/**
	 * @see org.apache.wicket.Application#getHomePage()
	 */
	@Override
	public Class<SasBasePage> getHomePage()
	{
		return SasBasePage.class;
	}
	
	@Override
	protected void init() {
		super.init();
		addComponentInstantiationListener(new SpringComponentInjector(this));
        //addPreComponentOnBeforeRenderListener(new AttachBehaviorListener(new FormStateCssBehavior()));
	}

	@Override
	public Session newSession(Request request, Response response) {
		SasSession session = new SasSession(request);
		 Locale locale = session.getLocale();
		 if (!LOCALES.contains(locale))
		 {
			 session.setLocale(Locale.ENGLISH);
		 }
		 return session;
	}
	
	@Override
	public RequestCycle newRequestCycle(Request request, Response response) {
		return new HibernateWebRequestCycle(this, (WebRequest) request, response, sessionFactory);
	}


}
